package sadmin

import (
	"encoding/json"
	"errors"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/config"
	"shop/models/sadmin"
	"strings"
)

type AppController struct {
	beego.Controller
}

//@Title appMsgupload
//@Description app信息入库
//@Param body body sadmin.App true "root为可见企业,status多久后发布"
//@Success 200 ok
//@router /appmsgupload [put]
func (c *AppController)AppMsgUpload(time string) {

	account:="admin"
	var com sadmin.App
	account,er:=c.token()
	if er!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=er.Error()
		goto f
	}
	json.Unmarshal(c.Ctx.Input.RequestBody,&com)
	//fmt.Println("cccccccccccccccccc",com,time)
	er=sadmin.AppMsgUpload(account,com)
	if er!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=er.Error()
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=com
	}
	f:
	c.ServeJSON()
}
//@Title getroot
//@Description 获取可见用户列表
//@Success 200 string
//@router /getroot/ [get]
func (c *AppController)GetRoot()  {

	account:="admin"
	account,er:=c.token()
	var ac []sadmin.User
	if er!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=er.Error()
		goto f
	}
	ac=sadmin.GetRoot(account)
	c.Ctx.ResponseWriter.WriteHeader(200)
	c.Data["json"]=ac
	f:
	c.ServeJSON()
}


//@Title appupload
//@Description 新增app时需要首先调用该方法，上传app到服务器，选中文件后即执行，用户不可见
//@Param file formData file true "file"
//@Success 200 ok
//@router /appuploadtoserver [post]
func (c *AppController)AppUploadToServer()  {
	url,err:=c.Upload()
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		//将该路径入库，并创建新的app
		err=sadmin.AppCreate(url)
		if err!=nil{
			c.Ctx.ResponseWriter.WriteHeader(201)
			c.Data["json"]=err.Error()
		}else {
			id:=sadmin.GetIdByUrl(url)
			c.Ctx.ResponseWriter.WriteHeader(200)
			c.Data["json"]=id
		}
	}
	c.ServeJSON()
}
//@Title appupload
//@Description 上传app到fir,用户可见,加载过程。。。
//@Param id path int true "app的id,第一步返回的id"
//@Success 200 ok
//@router /appuploadtofir/:id [post]
func (c *AppController)AppUploadToFir()  {
	id,_:=c.GetInt(":id")
	err:=sadmin.AppUpload(id)
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		//获取下载地址
		a:=sadmin.AppShowById(id)
		down:=sadmin.GetDownload(a.Name,a.Version)
		//将下载地址入库
		err=sadmin.DownUpdate(id,down)
		if err!=nil{
			c.Ctx.ResponseWriter.WriteHeader(201)
			c.Data["json"]=err.Error()
		}else {
			c.Ctx.ResponseWriter.WriteHeader(200)
			c.Data["json"] = "ok"
		}
	}
	c.ServeJSON()
}

//@Title changestatus
//@Description 执行监听任务，时间到之后执行上线操作
//@Param file query string true "app的路径"
//@Param time query string true "app多久之后上线,按小时计算，1小时之后传参为1"
//@Success 200 ok
//@router /changestatus [post]
func (c *AppController)ChangeStatus(file string ,time string)  {
	f:=make(chan int)
	go sadmin.ChangeStatus(file,time,f)
	<-f
	c.Ctx.ResponseWriter.WriteHeader(200)
	c.Data["json"]="ok"
	c.ServeJSON()
}
//@Title appshowbyid
//@Description 展示某个公司信息
//@Param id query int true "id"
//@Success 200 {object} sadmin.App
//@router /appshowbyid/ [get]
func (c *AppController)AppShowById(id int)  {
	com:=sadmin.AppShowById(id)
	c.Ctx.ResponseWriter.WriteHeader(200)
	c.Data["json"]=com
	c.ServeJSON()
}
//@Title appmsgshow
//@Description show app
//@Param status query string false "status,包括待发布，已上线，已下架"
//@Param typ query string false "type"
//@Param company query string false "管理员不用填写，超管可填写"
//@Success 200 {object} sadmin.App
//@router /appshowadmin [get]
func (c *AppController)AppMsgShowAdmin(status string,typ string,company string)  {
	//

	com,er:=sadmin.AppMsgShowSuperAdmin(status,typ,company)
	if er!=nil{
		c.Data["json"]="false"
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=com
	}

	c.ServeJSON()
}
//@Title AppMsgEditAdmin
//@Description 普通管理员接口
//@Param body body sadmin.App true "type,root,describe,newfeatures,status"
//@Success 200 ok
//@router /appeditadmin [put]
func (c *AppController)AppMsgEditAdmin()  {
	account:="admin"
	var com sadmin.App
	account,err:=c.token()
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
		goto f
	}
	json.Unmarshal(c.Ctx.Input.RequestBody,&com)
	err=sadmin.AppMsgEditAdmin(account,com)
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=com
	}
	f:
	c.ServeJSON()
}
//@Title appupload
//@Description 更新app新版本时需要首先调用该方法，上传app到服务器
//@Param file formData file true "file"
//@Param id path int true "id,第一步返回的id"
//@Success 200 ok
//@router /appserverupdate/:id [post]
func (c *AppController)AppUploadToServerUpdate()  {
	appid,_:=c.GetInt(":id")
	url,err:=c.Upload()
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		//将该路径入库，更新app的路径
		err=sadmin.AppUpdate(appid,url)
		if err!=nil{
			c.Ctx.ResponseWriter.WriteHeader(201)
			c.Data["json"]=err.Error()
		}else {
			c.Ctx.ResponseWriter.WriteHeader(200)
			c.Data["json"]="ok"
		}
	}
	c.ServeJSON()
}

//@Title apppicupdate
//@Description 上传图片更新,删除所有图片，然后进行更新
//@Param appid path int true "appid,第一步返回的id"
//@Success 200 ok
//@router /picuploadupdate/:appid [post]
func (c *AppController) PicUploadUpdate() {
	appid,_:=c.GetInt(":appid")
				err:=sadmin.PicDel(appid)
				//图片地址入库
				if err!=nil{
					c.Ctx.ResponseWriter.WriteHeader(201)
					c.Data["json"]=err.Error()
				}else {
					c.Ctx.ResponseWriter.WriteHeader(200)
					c.Data["json"]="ok"
				}
	c.ServeJSON()
}


//@Title AppMsgEditSuperAdmin
//@Description 超级管理员接口
//@Param body body sadmin.App true "type,root,describe,newfeatures,status,priority"
//@Success 200 ok
//@router /appeditsuperadmin [put]
func (c *AppController)AppMsgEditSuperAdmin()  {

	account:="admin"
	var com sadmin.App
	account,er:=c.token()
	if er!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=er.Error()
		goto f
	}
	json.Unmarshal(c.Ctx.Input.RequestBody,&com)
	er=sadmin.AppMsgEditSuperAdmin(account,com)
	if er!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=er.Error()
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=com
	}
	f:
	c.ServeJSON()
}
//@Title appeverypage
//@Description 动态分页---
//@Param num query int true "输入app每页展示的app数量，返回总页码数"
//@Success 200 float
//@router /appeverypage/ [get]
func (c *AppController)AppEveryPage(num int)  {
	f:=sadmin.AppEveryPage(num)
	c.Ctx.ResponseWriter.WriteHeader(200)
	c.Data["json"]=f
	c.ServeJSON()
}
//@Title apppaging
//@Description 动态分页---
//@Param num query int true "输入app每页展示的app数量"
//@Param page path int true "第几页的信息"
//@Param lx query string false "类型"
//@Param by query string false "非空按优先级排序，空按时间排序"
//@Param account query string false "账号"
//@Success 200 {object} sadmin.App
//@router /apppaging/:page [get]
func (c *AppController)AppPaging(num int,lx string,by string,account string)  {

	//通过账户来展示可见的app
	page,_:=c.GetInt(":page")
	a,er:=sadmin.AppPaging(num,page,by,lx,account)
	if er!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=er.Error()
	}else {
		c.Ctx.ResponseWriter.WriteHeader(200)
		c.Data["json"]=a
	}
	c.ServeJSON()
}
//@Title apppaging
//@Description 上传图片
//@Param file formData file true "file"
//@Param appid path int true "appid,第一步返回的id"
//@Success 200 ok
//@router /picupload/:appid [post]
func (c *AppController) PicUpload() {
	fie,head,err:=c.GetFile("file")
	appid,_:=c.GetInt(":appid")
	defer fie.Close()
	if err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(201)
		c.Data["json"]=err.Error()
	}else {
		name:=head.Filename
		//类型判断，改名
		name,err=sadmin.PicUpload(name)
		st,_:=config.NewConfig("ini","conf/app.conf")
		if err!=nil{
			c.Ctx.ResponseWriter.WriteHeader(201)
			c.Data["json"]=err.Error()
		}else {
			filename:=st.String("fileurl")+name
			err=c.SaveToFile("file",filename)
			if err!=nil{
				c.Ctx.ResponseWriter.WriteHeader(201)
				c.Data["json"]=err.Error()
			}else {
				//图片地址入库
				err=sadmin.PicUpdate(appid,filename)
				if err!=nil{
					c.Ctx.ResponseWriter.WriteHeader(201)
					c.Data["json"]=err.Error()
				}else {
					c.Ctx.ResponseWriter.WriteHeader(200)
					c.Data["json"]="ok"
				}

			}
		}

	}
c.ServeJSON()
}

//@Title apppaging
//@Description 上传图片
//@Param id path int true "appid,第一步返回的id"
//@Success 200 ok
//@router /iconupload/:id [post]
func (c *AppController) IconUpload() {
	appid,_:=c.GetInt(":id")
		name:="a.png"
		//类型判断，改名
		name,err:=sadmin.PicUpload(name)
		st,_:=config.NewConfig("ini","conf/app.conf")
		if err!=nil{
			c.Ctx.ResponseWriter.WriteHeader(201)
			c.Data["json"]=err.Error()
		}else {
			filename:=st.String("fileurl")+name
			//在该路径下创建文件，并解码到当前路径下
			err=sadmin.Pngencode(appid,filename)
			//err=c.SaveToFile("file",filename)
			if err!=nil{
				c.Ctx.ResponseWriter.WriteHeader(201)
				c.Data["json"]=err.Error()
			}else {
				//图片地址入库
				err=sadmin.IconUpdate(appid,filename)
				if err!=nil{
					c.Ctx.ResponseWriter.WriteHeader(201)
					c.Data["json"]=err.Error()
				}else {
					c.Ctx.ResponseWriter.WriteHeader(200)
					c.Data["json"]="ok"
				}

			}
		}

	c.ServeJSON()
}



func (c *AppController)Upload() (string, error) {
	_,head,_:=c.GetFile("file")
	name:=head.Filename
	//类型判断，改名
	name,err:=sadmin.PicUpload(name)
	st,_:=config.NewConfig("ini","conf/app.conf")
	if err!=nil{
		return "",err
	}else {
		filepath:=st.String("fileurl")
		//判断路径是否存在，不存在则创建
		_=sadmin.PathCreate(filepath)
		filename:=filepath+name
		err=c.SaveToFile("file",filename)
		if err!=nil{
				return "",err
		}
		return filename,nil
		}
}

func (c *AppController)token()(string,error) {
	account:="string"
	token:=c.Ctx.Input.Header("Authorization")
	str:=strings.Split(token,",")
	if len(str)!=2{
		return "",errors.New("token fail")
	}
	er:=sadmin.CheckToken(str[0])
	account=str[1]
	if er!=nil{
		return "",er
	}
	return account,nil
}